package beans;

import java.util.ArrayList;
import java.util.List;

import dao.Chapter;
import dao.ChapterDAO;

public class Chapterbean {
    private ChapterDAO chapterDAO;
    public Chapterbean(){
	chapterDAO = new ChapterDAO();
    }
    
    public boolean addChapter(Chapter chapter){
	return chapterDAO.save(chapter);
    }
    
    public boolean update(Chapter chapter){
	return chapterDAO.update(chapter);
    }
    
    public List<String> getChaptersByCourseId(String courseId){
	return chapterDAO.getChaptersByCourseId(courseId);
    }
    public Chapter getChapterById(String id){
	return chapterDAO.findById(id);
    }
    public List<String> getChapters(String courseId, int counts){
	ArrayList<String> chapters = new ArrayList<String>();
	String string = courseId;
	if(counts < 10){
	    string += "0";
	}	
	for(int i=1;i<=counts;i++){
	    chapters.add(string+i);
	}
	List<String> existedChapters = chapterDAO.getChaptersByCourseId(courseId);
	if(existedChapters.size() > 0){
	    for(int j=0;j<existedChapters.size();j++){
		for(int i=0;i<chapters.size();i++){
		    if(existedChapters.get(j).equals(chapters.get(i))){
			chapters.remove(i);
			break;
		    }
		}
	    }
	}
	return chapters;
    }
    public static void main(String[] args){
	Chapterbean chapterbean = new Chapterbean();
	Chapter chapter = new Chapter();
	chapter.setChapterid("010104");
	chapter.setCourseid("0101");
	chapter.setDemand("宇宙的起源");
	chapter.setName("宇宙官史");
	chapter.setStructurechart("/image");
	chapterbean.addChapter(chapter);
	System.out.println(chapterbean.getChapterById("010104").getDemand());
    }
}
